package com.googlecode.gwt4gae.client.home;

import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.gwt4gae.client.BaseView;
import com.googlecode.gwt4gae.client.Gwt4gaeConstants;

import com.extjs.gxt.themes.client.Slate;
import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HtmlContainer;
import com.extjs.gxt.ui.client.widget.Viewport;
import com.extjs.gxt.ui.client.widget.custom.ThemeSelector;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

public class HomeView extends BaseView {		
	private Viewport viewport;
	private ContentPanel centerPanel;
	private HtmlContainer northPanel;
	private ContentPanel westPanel;

	public HomeView(HomeAction action) {
		super(action);
	}

	@Override
	protected void initialize() {
		viewport = new Viewport();
	    viewport.setLayout(new BorderLayout());
	    Registry.register(Gwt4gaeConstants.VIEWPORT, viewport);
	    
	    createNorth();
	    createWest();
	    createCenter();
	    
	    RootPanel root = RootPanel.get("content");
        // be friendly to tests
        if (root == null) {
            root = RootPanel.get();
        }
        
        root.add(viewport);
	}
	
	private void createCenter() {
	    centerPanel = new ContentPanel();
	    centerPanel.setBorders(false);
	    centerPanel.setHeaderVisible(false);
	    centerPanel.setLayout(new FitLayout());

	    BorderLayoutData data = new BorderLayoutData(LayoutRegion.CENTER);
	    data.setMargins(new Margins(5, 5, 5, 0));
	    viewport.add(centerPanel, data);
	    Registry.register(Gwt4gaeConstants.CENTER_PANEL, centerPanel);
	  }

	  private void createWest() {
	    BorderLayoutData data = new BorderLayoutData(LayoutRegion.WEST, 220, 150, 320);
	    data.setMargins(new Margins(5, 5, 5, 5));
	    data.setCollapsible(true);
	    westPanel = new ContentPanel();
	    
	    viewport.add(westPanel, data);
	    Registry.register(Gwt4gaeConstants.WEST_PANEL, westPanel);
	  }

	  private void createNorth() {
	    StringBuffer sb = new StringBuffer();
	    sb.append("<div id='north-theme'></div><div id=demo-title>Gwt4GAE Framework</div>");

	    northPanel = new HtmlContainer(sb.toString());
	    northPanel.setStateful(false);
	    northPanel.setId("north-header");
	    northPanel.addStyleName("x-small-editor");
	    ThemeManager.register(Slate.SLATE);
	    ThemeSelector selector = new ThemeSelector();
	    selector.setWidth(125);
	    northPanel.add(selector, "#north-theme");

	    BorderLayoutData data = new BorderLayoutData(LayoutRegion.NORTH, 33);
	    data.setMargins(new Margins());
	    viewport.add(northPanel, data);
	    Registry.register(Gwt4gaeConstants.NORTH_PANEL, northPanel);
	  }

}
